package com.mkz.jbpm.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mkz.core.common.util.ResponseResult;
import com.mkz.core.security.util.SecurityUtils;
import com.mkz.jbpm.activiti.ActInstanceService;
import com.mkz.jbpm.entity.LeaveInfo;
import com.mkz.jbpm.mapper.LeaveInfoMapper;
import com.mkz.jbpm.service.LeaveInfoService;

import lombok.AllArgsConstructor;

import java.util.Map;

import org.activiti.engine.history.HistoricTaskInstance;
import org.springframework.stereotype.Service;

/**
 * ClassName: LeaveInfoServiceImpl 
 * @Description: 请假信息实现类
 * @author: KuangJianyu
 * @date: 2020年4月4日
 */
@Service
@AllArgsConstructor
public class LeaveInfoServiceImpl extends ServiceImpl<LeaveInfoMapper, LeaveInfo> implements LeaveInfoService {
    private final ActInstanceService actInstanceService;
	
	public ResponseResult<Boolean> startProcess(String proDefKey, Object busObj, Map<String, Object> vars) throws Exception {
		LeaveInfo leaveInfo = (LeaveInfo)busObj;
		Map<String, Object> startResult = actInstanceService.startWithBusinessKeyAndExecuteFirst(proDefKey, leaveInfo.getId().toString(), SecurityUtils.getUser().getUsername(), vars);
		HistoricTaskInstance activeTask = (HistoricTaskInstance)startResult.get("active");
		leaveInfo.setProcInstId(activeTask.getProcessInstanceId());
		boolean saveResult = this.updateById(leaveInfo);
		if(!saveResult) {
			ResponseResult.failed("请假信息保存失败！");
		}
		return ResponseResult.ok(true);
	}
}
